package hdd.webScoket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

@Component
public class MyWebSocketHandler extends AbstractWebSocketHandler {
    private static  final Logger logger = LoggerFactory.getLogger( MyWebSocketHandler.class );

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        super.afterConnectionEstablished( session );
        logger.info( "webScoket connected " );
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        super.afterConnectionClosed( session, status );
        logger.info( "webScoket connection closed   " );
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        super.handleTextMessage( session, message );
        logger.info( "收到： "+message.getPayload() );
        Thread.sleep( 1000 );//模拟延时
        session.sendMessage( new TextMessage( "  加油 " ) );//发送
    }
}
